package com.web.servlet;

import com.web.entity.TestBiao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 该Servlet负责添加考试到test表中
 * @author 12270
 */
@WebServlet("/atss")
public class AddTestServlet extends HttpServlet{
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.获取请求参数
        resp.setContentType("application/x-json;charset=utf-8");
        //获取新申请的考试的信息，封装成一个考试对象
        req.setCharacterEncoding("utf-8");
        int testid = Integer.parseInt(req.getParameter("testid"));
        int tid = Integer.parseInt(req.getParameter("tid"));
        String testtime = req.getParameter("testtime");
        int courseid = Integer.parseInt(req.getParameter("courseid"));
        int classid = Integer.parseInt(req.getParameter("classid"));
        int istestfinish = Integer.parseInt(req.getParameter("istestfinish"));
        HttpSession session = req.getSession();
        session.setAttribute("testid",testid);//保留testid 让管理员获取
        


        //获取考试的6项信息，封装成一个newTest对象
        TestBiao newTest = new TestBiao(testid,tid,testtime,courseid,classid,istestfinish);

        //2. 执行业务逻辑
        //将新考试插入数据库
        String driver_class = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://localhost:1433;databaseName=OnlineTestSystem";
        String username = "sa";
        String password = "sa";
        
        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0; //受影响行数
        System.out.println(rows);
        
        try {
            Class.forName(driver_class);
            con = DriverManager.getConnection(url, username, password);
            ps = con.prepareStatement("insert into test values(?,?,?,?,?,?)");
            ps.setInt(1, newTest.getTestid());
            ps.setInt(2, newTest.getTid());
            ps.setString(3, newTest.getTesttime());
            ps.setInt(4, newTest.getCourseid());
            ps.setInt(5, newTest.getClassid());
            ps.setInt(6, newTest.getIstestfinish());
            rows = ps.executeUpdate();//返回受影响的行数
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != ps) {
                    ps.close();
                }
                if (null != con) {
                    con.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }    
        //3. 生成响应内容，这里直接返回了受影响的行数，用于判断是否插入成功
        resp.getWriter().println(rows);
        System.out.println(rows);
    } 
}
